﻿using System;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Admin.Categories
{
    public partial class List : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) {
                GetCategories();
            }
        }

        private void GetCategories() {
            var categories = EnquiryCategoryRepository.QueryOver().List();
            CategoryList.DataSource = categories.OrderBy(c => c.Department.DepartmentName).ThenBy(c => c.CategoryName);
            CategoryList.DataBind();
        }

        protected void CategoryListItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item &&
                e.Item.ItemType != ListItemType.AlternatingItem) {
                return;
            }
            var rowCategory = (EnquiryCategory)e.Item.DataItem;
            var tableRow = (HtmlTableRow)e.Item.FindControl("CategoryRow");
            var editLink = (HyperLink)e.Item.FindControl("EditLink");

            if (!rowCategory.AvailableForSelection)
            {
                tableRow.Attributes.Add("class", "disabled");
            }
            editLink.NavigateUrl = "~/Admin/Categories/Edit.aspx?id=" + rowCategory.Id;
        }
    }
}